package cn.wk.servlet;

import cn.wk.entity.Admins;
import cn.wk.entity.Teacher;
import cn.wk.services.AdminsServices;
import cn.wk.services.TeacherServices;
import cn.wk.services.impl.AdminsServicesImpl;
import cn.wk.services.impl.TeacherServicesImpl;
import cn.wk.util.JsonRes;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/teacheradd")
public class Servletteachersadd extends HttpServlet {
    private static TeacherServices as=new TeacherServicesImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1. 接收之前转换
           request.setCharacterEncoding("utf-8");
           //2. 接收
        Map<String, String[]> map = request.getParameterMap();
        String s = request.getParameter("status");

        Teacher teacher=new Teacher();

        try {
            BeanUtils.populate(teacher,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int b=0;
        if(s!=null){
            teacher.setStatus(1);
        }else {
            teacher.setStatus(0);
        }
        if(teacher!=null){
            if(teacher.getTid()>0){
                b= as.MidffTeacher(teacher.getTpwd(),teacher.getTname(),teacher.getTgender(),
                        teacher.getTaddress(),teacher.getTtel(),teacher.getTemail(),teacher.getStatus(),teacher.getTid());
            }else{
                b= as.AddTeacher(teacher.getTno(),teacher.getTpwd(),teacher.getTname(),
                        teacher.getTgender(),teacher.getTaddress(),teacher.getTtel(),
                        teacher.getTemail(),teacher.getStatus());
            }
        }
        if(b>0)
            response.getWriter().println(JsonRes.res());
        else
            response.getWriter().println(JsonRes.error());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
